﻿using Appliaction.Dto.Pinv;
using Appliaction.IAppServices.Pinv;
using AutoMapper;
using Domain.Entitys.Pinv;
using Domain.IRepositorys.Pinv;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Appliaction.AppServices.Pinv
{
    public class PbService : IPbService
    {
        private readonly IPbRepository pbRepository;
        private readonly IMapper mapper;

        public PbService(IPbRepository pbRepository,IMapper mapper)
        {
            this.pbRepository = pbRepository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 条件查询采购入库总条数
        /// </summary>
        /// <param name="Pb_code"></param>
        /// <param name="Vendor_name"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int GetPbCount(string Pb_code, string Vendor_name)
        {
            return pbRepository.GetPbCount(Pb_code, Vendor_name);
        }

        public int GetPbCountinfo(string Pb_code, string Vendor_name)
        {
            return pbRepository.GetPbCountinfo(Pb_code, Vendor_name);
        }

        /// <summary>
        /// 分页条件查询采购入库数据
        /// </summary>
        /// <param name="Pb_code"></param>
        /// <param name="Vendor_name"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Pb_details_Order> GetPbFyWhere(string Pb_code, string Vendor_name, int currentPage, int pageSize)
        {
            return pbRepository.GetPbFyWhere(Pb_code, Vendor_name, currentPage, pageSize);
        }

        public List<Pb_details_Order> GetPbFyWhereinfo(string Pb_code, string Vendor_name, int currentPage, int pageSize)
        {
            return pbRepository.GetPbFyWhereinfo(Pb_code, Vendor_name, currentPage, pageSize);
        }

        /// <summary>
        /// 根据code查询明细
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<List<Pinv_purwhsedetail>> Getpurwhsedetailwherecode(string code)
        {
            return await pbRepository.Getpurwhsedetailwherecode(code);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="pinvOrderPbDetailsDto"></param>
        /// <returns></returns>
        public Task<int> PostPbAdd(PinvOrderPbDetailsDto<Pinv_Pb_AddDto> pinvOrderPbDetailsDto)
        {
            Pinv_pb pb = mapper.Map<Pinv_pb>(pinvOrderPbDetailsDto.Pinv_Pb_Order_Dto);
            List<Pinv_purwhsedetail> pinv_Purwhsedetails = mapper.Map<List<Pinv_purwhsedetail>>(pinvOrderPbDetailsDto.pinv_Pb_DetailsDtos);
            return pbRepository.PostPbAdd(pb, pinv_Purwhsedetails);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="pinv_Pb"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> PutPbDelete(PbDeleteDto pinv_Pb)
        {
            Pinv_pb pb = mapper.Map<Pinv_pb>(pinv_Pb);
            return pbRepository.PutPbDelete(pb);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="pbUpdateDtos"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> PutPbUpdate(Pb_details_Update_Dto<PbUpdateDto> pbUpdateDtos)
        {
            Pinv_pb pb = mapper.Map<Pinv_pb>(pbUpdateDtos.Pb_Update_Dto);
            List<Pinv_purwhsedetail> details = mapper.Map<List<Pinv_purwhsedetail>>(pbUpdateDtos.pbdetailsUpdateDtos);
            return pbRepository.PutPbUpdate(pb, details);
        }

        public async Task<int> PutPodetails_Pb_PbdetailsUpdate(Po_Audit_Dto pinv_Po, List<Po_details_Audit_Dto> pinv_Details, Pb_Audit_Dto pinv_Pb, List<Pb_details_Audit_Dto> pinv_Purwhsedetail)
        {
            // 映射单个对象
            Pinv_Po po = mapper.Map<Pinv_Po>(pinv_Po);
            Pinv_pb pb = mapper.Map<Pinv_pb>(pinv_Pb);

            // 映射集合对象（关键修改点）
            List<pinv_details> pdList = mapper.Map<List<pinv_details>>(pinv_Details);
            List<Pinv_purwhsedetail> pdsList = mapper.Map<List<Pinv_purwhsedetail>>(pinv_Purwhsedetail);

            return await pbRepository.PutPodetails_Pb_PbdetailsUpdate(po, pdList, pb, pdsList);
        }

        public Task<int> PutPoUpdateAudit(Pb_Audit_Dtos pinv_pb)
        {
            Pinv_pb pb = mapper.Map<Pinv_pb>(pinv_pb);
            return pbRepository.PutPoUpdateAudit(pb);
        }
    }
}
